package lsl.leetCode.year2023.test0204_Bisection;


/**
 * 33. 搜索旋转排序数组
 *
 * @author Zed
 * @description
 * @date 2023/2/4 21:01
 */
public class test33 {
    public static void main(String[] args) {
        int[] nums = {5, 1, 3};
        int target = 5;
        search(nums, target);
    }

    public static int search(int[] nums, int target) {
        int right = nums.length - 1;
        int left = 0;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[0] <= nums[mid]) {
                if (nums[0] <= target && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[nums.length - 1]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return -1;
    }
}
